<template>
	<view class="mainpadding beijingse" v-if="pageShow">
		<!-- 无地址 -->
		<view class=" ffffff radius flexcenter" style="padding:60rpx ;" v-if="addressData==null"  @click="tzshdz">
			<view class="xiaocheng titletext fonweight textcenter">点击添加收货地址</view>
		</view>
		<!-- 有地址 -->
		<view class="mainpadding ffffff radius flexbetween" v-if="addressData!=null"  @click="tzshdz">
			<view class="">
				<view class="flexleft">
					<view class="titletext fonweight xiaohei margin_right4">{{addressData.name}}</view>
					<view class="titletext fonweight xiaohei">{{addressData.mobile}}</view>
				</view>
				<view class="ershil xiaohui margin_top2">{{addressData.cityinfo}}{{addressData.address_detail}}</view>
			</view>
			<u-icon name="arrow-right" color="#999999"></u-icon>
		</view>
		<view class="flexbetween mainpadding ffffff radius margin_top" v-for="item in orderData.lists" :key="item.goodsinfo.id">
			<view class="sptp">
				<image class="" :src="item.goodsinfo.image_text" mode=""></image>
			</view>
			<view class="" style="width: 75%;">
				<view class="titletext xiaohei yhxk">{{item.goodsinfo.name}}</view>
				<view class="flexbetween margin_top">
					<view class="bigtext fonweight xiaocheng " v-if="lastmsg.ly!=3">¥{{item.goodsinfo.price}}</view>
					<view class="bigtext fonweight xiaocheng " v-if="lastmsg.ly==3">¥{{item.goodsinfo.fg_price}}</view>
					<view class="ershil fonweight xiaohui ">×{{item.number}}</view>
				</view>
			</view>
		</view>
		<!-- 备注 -->
		<view class="ffffff margin_top radius mainpadding">
			<view class="ershil fonweight xiaohei ">备注</view>
			<textarea class="margin_top2 textarea" placeholder="请输入留言信息给卖家" v-model="remarks"></textarea>
		</view>
		<!-- 固定底部 -->
		<view class="gudingdb ffffff mainpadding">
			<view class="flexbetween">
				<view class="titletext xiaohei">合计：
					<text class="bigtext xiaocheng fonweight">¥{{orderData.statis.pay_price}}</text>
				</view>
				<view class="libtn flexcenter" @click="pay">立即支付</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				lastmsg: {
					type: '',
					ly:"",//1=普通商品,2=换货,3=复购专区,4=会员购买(换货支付使用货款，复购专区金额显示低的价格划线价显示price)
					data: [],
					user_level_id:"",
				},
				remarks: "",
				addressid: "",
				orderData: {},
				addressData: {},
				pageShow: false
			}
		},
		onLoad(options) {
			this.lastmsg = JSON.parse(decodeURIComponent(options.msg));
		},
		onShow() {
			uni.$off('checkAddressId')
			uni.$on('checkAddressId', data => {
				this.addressid = data
			})
			this.getdingdan()
		},
		methods: {
			pay() {
				if (this.addressid == "") {
					httpRequest.toast("请选择地址")
					return
				}
				uni.showLoading({
					title: '请稍候...'
				})
				let lists = []
				this.orderData.lists.forEach(item => {
					lists.push({
						goods_id: item.goodsinfo.id,
						number: item.number
					})
				})
				let data = {
					address_id: this.addressid, //地址ID
					remarks: this.remarks,
					ly: this.lastmsg.ly,
					data: lists
				}
				if(this.lastmsg.ly==4){
					this.$set(data,"user_level_id",this.lastmsg.user_level_id)
				}
				httpRequest.request("/api/order/addOrder", 'POST', data, false, false, true).then(res => {
					if (res.code == 1) {
						if (this.lastmsg.type == "cart") {
							this.delcar()
						}
						let obj = {
							order_id: res.data.order_id,
							pay_type: this.lastmsg.ly==2?20:10,
						}
						this.payment(obj)
					} else {
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					uni.hideLoading()
					httpRequest.toast(err.msg);
				})
			},
			payment(data){
				httpRequest.request("/api/order/payment", 'POST', data, false, false, true).then(res => {
					uni.hideLoading()
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},500)
					}
					httpRequest.toast(res.msg)
				}).catch(err => {
					uni.hideLoading()
					httpRequest.toast(err.msg);
				})
			},
			delcar() {
				let arr = this.lastmsg.data
				arr.forEach(item => {
					delete item.number
				})
				httpRequest.request('/api/cart/delCart', 'POST', {
					data: arr,
					data_type:this.lastmsg.ly
				}).then(res => {})
			},
			getdingdan() {
				let data = {
					address_id: this.addressid, //地址ID
					type: this.lastmsg.type, //来源类型detail详情，car购物车
					ly: this.lastmsg.ly,
					data: this.lastmsg.data //数据列表
				}
				httpRequest.request("/api/order/getOrderGoodsList", 'POST', data, false, false, true).then(res => {
					if (res.code == 1) {
						this.orderData = res.data.orderData
						this.addressData = res.data.addressData
						if (res.data.addressData) {
							this.addressid = res.data.addressData.id
						}
						this.pageShow = true
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			// 跳转收货地址
			tzshdz() {
				uni.navigateTo({
					url: '/pages_homepage/shouhuodizhi?type=1'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.libtn {
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		width: 250rpx;
		height: 76rpx;
		background: #FF9341;
		border-radius: 120rpx 120rpx 120rpx 120rpx;
	}

	.textarea {
		height: 100rpx;
	}

	.sptp {
		width: 134rpx;
		height: 134rpx;
		border-radius: 14rpx;

		image {
			border-radius: 14rpx;
			width: 100%;
			height: 100%;
		}
	}
</style>